<?php

namespace STAGE\StageBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use STAGE\StageBundle\Entity\Stage;
use STAGE\StageBundle\Form\StageType;
use Symfony\Component\HttpFoundation\Response;

class StageController extends Controller {

    public function getEtidiantAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getManager();
        $term = $request->get('name_startsWith');
        $etudiants = $em->getRepository('UserBundle:User')->getEtudiant($term);
        $result = array();
        if (count($etudiants) > 0) {
            foreach ($etudiants as $ete) {
                $e = array();
                $e['id'] = $ete->getId();
                $e['name'] = $ete->getNom() . ' ' . $ete->getPrenom();
                $result[] = $e;
            }
        }
        return new Response(json_encode($result));
    }

    public function indexAction() {
        return $this->render('StageBundle:Stage:index.html.twig');
    }

    public function newAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getManager();
        $id = $request->get('id');
        if ($id == '')
            $stage = new Stage();
        else
            $stage = $em->getRepository('StageBundle:Stage')->find($id);
        $form = $this->createForm(new StageType(), $stage);
        if ($request->isMethod('post')) {
            $form->bind($request);
//            if($form->isValid()){
            $stage = $form->getData();
            $em->persist($stage);
            $em->flush();
            $url = $this->generateUrl('stage_stage_list');
            return $this->redirect($url);
//            }
        }
        return $this->render('StageBundle:Stage:new.html.twig', array('id' => $id, 'form' => $form->createView()));
    }

    public function listAction() {
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        if ($this->get('security.context')->isGranted('ROLE_ADMIN'))
            $stages = $em->getRepository('StageBundle:Stage')->findAll();
        if ($this->get('security.context')->isGranted('ROLE_ENSEIGNANT'))
            $stages = $this->getUser()->getStages();
        if ($this->get('security.context')->isGranted('ROLE_ETUDIANT'))
            $stages = $this->getUser()->getStageEtudiant();
        if ($this->get('security.context')->isGranted('ROLE_ENCADREUR'))
            $stages = $this->getUser()->getStageEncadreur();
        if ($request->get('type') != '')
            return $this->render('StageBundle:Stage:page_list.html.twig', array('stages' => $stages));
        return $this->render('StageBundle:Stage:list.html.twig', array('stages' => $stages));
    }

    public function deleteAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getManager();
        $id = $request->get('id');
        $stage = $em->getRepository('StageBundle:Stage')->find($id);
        $em->remove($stage);
        $em->flush();
        $url = $this->generateUrl('stage_stage_list', array('type' => 'ajax'));
        return $this->redirect($url);
    }
    public function afficherAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getManager();
        $id = $request->get('id');
        $stage = $em->getRepository('StageBundle:Stage')->find($id);
        return $this->render('StageBundle:Stage:afficher.html.twig', array('stage' => $stage));
    }

}
